rm(list = ls())

if (!require("pacman")) install.packages("pacman")
pacman::p_load(
    tidyverse,
    fixest,
    readxl,
    rdrobust,
    knitr,
    broom
)

conflicted::conflicts_prefer(dplyr::filter, dplyr::select)

# Load and prepare data
polbar <- read_rds("data/pb_2016_clean.rds") %>%
    haven::zap_labels() %>%
    dplyr::rename(male_bin = male, east_bin = east) %>%
    mutate(
        men = ifelse(male_bin == 1, "Men", "Women"),
        east = ifelse(east_bin == 1, "East Germany", "West Germany"),
        post = ifelse(age_categ <= 6, 1, 0),
        indoctrination = yob * post,
        sat_mw = sat_mw * 100,
        sat_democ = sat_democ * 100
    ) %>%
    filter(age_categ %in% 2:9)

# Table 2, rows 5 and 6: East -------------------------------------------------------------------------

m1 <- feols(sat_mw ~ yob + post + indoctrination,
    split = ~men,
    data = polbar %>% filter(east_bin == 1)
)

# Format East results
east_results <- lapply(seq_along(m1), function(i) {
    model <- m1[[i]]
    ct <- coeftable(model)
    indoctrination_row <- ct[rownames(ct) == "indoctrination", ]
    tibble(
        outcome = "Satisfaction with market economy",
        gender = ifelse(i == 1, "Men", "Women"),
        coef = indoctrination_row[1],
        ci_low = indoctrination_row[3],
        ci_high = indoctrination_row[4],
        N = nobs(model)
    )
}) %>% bind_rows()

east_table_formatted <- east_results %>%
    mutate(coef_ci = sprintf("%.2f [%.2f, %.2f]", coef, ci_low, ci_high)) %>%
    select(outcome, gender, coef_ci, N)

print("East Germany Results:")
knitr::kable(east_table_formatted, format = "pipe", caption = "East Germany: Satisfaction with Market Economy")

# Table 3, rows 5 and 6: West -------------------------------------------------------------------------

m2 <- feols(sat_mw ~ yob + post + indoctrination,
    split = ~men,
    data = polbar %>% filter(east_bin == 0)
)

# Format West results
west_results <- lapply(seq_along(m2), function(i) {
    model <- m2[[i]]
    ct <- coeftable(model)
    indoctrination_row <- ct[rownames(ct) == "indoctrination", ]
    tibble(
        outcome = "Satisfaction with market economy",
        gender = ifelse(i == 1, "Men", "Women"),
        coef = indoctrination_row[1],
        ci_low = indoctrination_row[3],
        ci_high = indoctrination_row[4],
        N = nobs(model)
    )
}) %>% bind_rows()

west_table_formatted <- west_results %>%
    mutate(coef_ci = sprintf("%.2f [%.2f, %.2f]", coef, ci_low, ci_high)) %>%
    select(outcome, gender, coef_ci, N)

print("West Germany Results:")
knitr::kable(west_table_formatted, format = "pipe", caption = "West Germany: Satisfaction with Market Economy")

# East: treat * gender interaction p value -------------------------------------------------------------------------

m3 <- feols(sat_mw ~ yob + post + indoctrination * men,
    data = polbar %>% filter(east_bin == 1)
)

pval_pb <- m3 %>%
    tidy() %>%
    filter(term == "indoctrination:menWomen") %>%
    pull(p.value) %>%
    as.numeric()

cat("P-value for Sat. w/ market economy (PB): ", pval_pb, "\n")
